package com.example.demo.mp.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.mp.entity.Announcement;
import com.example.demo.mp.entity.Userinfo;
import com.example.demo.mp.entity.UserinfoDTO;
import com.example.demo.mp.service.AnnouncementService;
import com.example.demo.tool.RedisTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Controller
@RequestMapping("Announcement")
public class AnnouncementController {

    @Resource
    AnnouncementService service;

    @Resource
    RedisTool redisTool;

    @GetMapping("/getAnnouncementInfo")
    public String getAnnouncementInfo(Long id,Model model) {
        Announcement announcement= service.getAnnouncementById(id);
        model.addAttribute("name", announcement.getTitle());
        model.addAttribute("content", announcement.getContent());
        return "index"; // 返回的是 templates 目录下的 index.html 文件
    }

    @GetMapping("selectAll")
    public R selectAll(Page page, UserinfoDTO userinfo) {
        IPage<Announcement> iPage=  redisTool.get("announcementPage:"+page.getCurrent(), IPage.class);
        if(iPage==null){
            iPage= this.service.page(page);
            redisTool.set("announcementPage:"+page.getCurrent(),iPage,600);
        }
        return R.ok(iPage);
    }
}
